package com.konne.nightmare.FastPublicOpinion.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:下午 2:42.
 * description: ** .
 */
public class SheetDialog extends Dialog {
    private LinearLayout contentView;
    private List<Sheet> sheets;
    private SheetListener sheetListener;

    public SheetDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = new LinearLayout(getContext());
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        contentView.setOrientation(LinearLayout.VERTICAL);
        initView();
        setContentView(contentView);
        Window dialogWindow = getWindow();
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialogWindow.getDecorView().setPadding(SizeUtils.dp2px(10), 0, SizeUtils.dp2px(10), SizeUtils.dp2px(10));
        dialogWindow.setGravity(Gravity.BOTTOM);

    }

    private void initView() {
        contentView.removeAllViews();
        if (sheets == null) {
            return;
        }
        for (int i = 0; i < sheets.size(); i++) {
            contentView.addView(getSheetBtn(sheets.get(i), i));
            if (i < sheets.size() - 1) {
                View line = new View(getContext());
                line.setBackgroundColor(0xffDFE1E2);
                contentView.addView(line, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(1)));
            }
        }
    }

    private SuperTextView getSheetBtn(Sheet sheet, int index) {
        SuperTextView stv = new SuperTextView(getContext());
        stv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(50)));
        if (index == 0) {
            stv.setCorner(SizeUtils.dp2px(13));
            stv.setLeftTopCornerEnable(true);
            stv.setRightTopCornerEnable(true);
        }
        if (index == sheets.size() - 1) {
            stv.setCorner(SizeUtils.dp2px(13));
            stv.setLeftBottomCornerEnable(true);
            stv.setRightBottomCornerEnable(true);
        }
        stv.setText(sheet.txt);
        stv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        stv.setTextColor(sheet.textColor==0?0xff1C2B33:sheet.textColor);
        stv.setGravity(Gravity.CENTER);
        stv.setPressBgColor(Color.WHITE);
        stv.setSolid(0xefffffff);
        stv.setTag(sheet);
        stv.setOnClickListener(btnClick);
        return stv;
    }
    private View.OnClickListener btnClick=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Sheet sheet= (Sheet) view.getTag();
            if(sheetListener!=null){
                sheetListener.sheet(sheet);
            }
            dismiss();
        }
    };
    public SheetDialog addSheet(Sheet sheet) {
        if (sheets == null) {
            sheets=new ArrayList<>();
        }
        sheets.add(sheet);
        return this;
    }

    public SheetDialog addSheets(List<Sheet> sheets) {
        this.sheets = sheets;
        return this;
    }

    public SheetDialog setSheetListener(SheetListener sheetListener) {
        this.sheetListener = sheetListener;
        return this;
    }

    public interface SheetListener {
        void sheet(Sheet sheet);
    }

    public static class Sheet {
        public String txt;
        public int textColor;
        public int action;

        public Sheet(String txt, int textColor, int action) {
            this.txt = txt;
            this.textColor = textColor;
            this.action = action;
        }
    }
}
